Generating Token for Apple Music API

Generating Token for Apple Music API

Hi, this is Charu from Classmethod. In this blog we will be seeing how to generate a token if you want to access Apple Music API step by step.

Let's get started!

To get started you need to have the access to the Apple Developer Program . It is not free of cost, hence you should purchase it's yearly plan and then proceed.

Let's proceed with the steps assuming that you have purchased the program.

You are supposed to note down few credentials before moving on, and these are Team ID, Key ID and Auth Key File(in .p8 file format). Let's see how to get them,

Team ID

Under you dashboard, click on Account and scroll down till the end. You can see your Membership Details which also includes Team ID as shown in the picture.

Key ID

For Key ID, you need to first make an Identifier. For that, go to you Account section and select Identifiers as shown in the picture.

Next, click on the Plus(+)button just beside the Identifier and select the type of ID you want to create. I am selecting Music ID. Then click on Continue. Enter the details asked and Register.

Now let's create the key. Go to Keys and click on the Plus(+) button just beside the Keys. Give a name and register your key.

After creating the key, you need to select the Identifier as shown in the picture below-

Click Save > Continue > Register and download you Key. We will be needing it in the code.

As you have collected all the necessary information, you can write the code below to generate the token,

//route.ts
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

export const appleMusicRoutePrefix = 'applemusic';
import { appleLoginSchema } from './schema';

export const appleRoutes = async (server: FastifyInstance) => {
  server.get('/get-token', {
    schema: appleLoginSchema,
    handler: async (
      _: FastifyRequest,
      reply: FastifyReply,
    ) => {
      const { getToken } = require('apple-music-token-node');
      const path = require('path');
      const keyId = 'YOUR-KEY-ID';
      const teamId = 'YOUR-TEAM-ID';
      const certPath = path.resolve(__dirname, './AuthKey_ABCDE12345.p8');
      console.log('Resolved certPath:', certPath);
      const token = getToken(certPath,teamId,keyId);

      return reply.code(200).send(token);
    },
  })
}
//schema.ts
export const appleLoginSchema = {
  response: {
    200: {
      type: 'object',
      additionalProperties: true
    },
  },
};

The above code snippets are parts of a Node.js application using the Fastify framework, designed to create an API endpoint for generating Apple Music tokens. It defines a route /get-token using Fastify. This function uses the apple-music-token-node module to generate a token, requiring a .p8 certificate file and specific Apple Developer account identifiers (keyId, teamId). The generated token is then sent back to the client with an HTTP 200 status code, indicating a successful request.

You can refer to this blog to generate Apple Music Tokens in your application. This setup is essential for applications that need to authenticate and interact with Apple Music's API.

Thank you reading!

Happy Learning:)

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.